<?php

use swap\visitor;
use swap\lazy_checker;
use swap\check_failed;
use swap\service_except;
use swap\loader;

class site_controller extends swap\controller {

    public static $layout = 'main';

    public static function index_action() {
        self::show_page();
    }

    public static function do_login_action() {
        // 拦截
        self::method('post');

        try {
            // 校验
            $checker = new lazy_checker(p());
            $checker->check_model_rules('user', user_model::operation_login);
            $post = $checker->get_all();
            extract($post);

            // captcha校验
            try {
                user_service::check_captcha($validate);
            } catch (service_except $se) {
                $checker->failed($se->get_prop_name(), $se->get_msg());
            }

            // db校验
            $user = user_model::get_one(array('email' => $email));
            if ($user === null) {
                $reg_url = url('my-site/reg');
                $checker->failed('email', '该E-mail地址尚未注册' . "<br /><a href='{$reg_url}'>立即注册</a>");
            }
            if (!$user->is_valid_password($password, $user->salt)) {
                $checker->failed('password', '密码错误!');
            }

            // login time
            if (p_has('isautologin')) {
                $login_seconds = 1209600; // 2 weeks
            } else {
                $login_seconds = 0;
            }

            // 设置
            visitor::set_role('user', $user->id, $login_seconds, array('email' => $email));

            // 发送
            self::json_result(true, '', 1, url('my-user/index'));
        } catch (check_failed $e) {
            $message = $e->get_reasons();
            self::json_result(false, current($message), 0, key($message));
        }
    }

    public static function test_action() {
        self::send_page();
    }

    public static function dev_action() {
        
        
//        resume_model::inc_resume_degree(1, 'myself');
        
        
        
//        phpinfo();exit;
//        $host = 'www.baidu.com';
//        $port = 80;
//        $result = fsockopen($host, $port);
//        var_dump($result);exit;
        
//        $last_resume = resume_model::get_last_resume();
//        dump($last_resume);
        
//        $training = resume_ext_service::get_resume_ext(11, 'training');
//        dump($training);
        
        
//        $resume_id = 11;
//        $type = 'training';
//        $id = 3;
//        try {
//            resume_ext_service::set_resume_ext($resume_id, $type, $id);
//        } catch (resume_ext_except $e) {
//            var_dump($e->get_msg());exit;
//        }
        
        
//        var_dump('');exit;
//        var_dump(null);exit;
        
//        loader::load_third('phpmailer/class.phpmailer.php');
//        $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
//        $mail->IsSMTP(); // telling the class to use SMTP
//        try {
//            $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
//            $mail->SMTPAuth = true;                  // enable SMTP authentication
//            $mail->Host = "smtp.qq.com";      // sets GMAIL as the SMTP server
//            $mail->Username = "529898521@qq.com";  // GMAIL username
//            $mail->Password = "cgh_0932";      // GMAIL password
//            $mail->AddAddress('hinson0@qq.com', 'hinson0');
//            $mail->SetFrom('529898521@qq.com', 'cgh');
//            $mail->Subject = 'test';
//            $mail->MsgHTML('test');
//            $mail->Send();
//            echo "Message Sent OK</p>\n";exit;
//        } catch (phpmailerException $e) {
//            echo $e->errorMessage(); exit;//Pretty error messages from PHPMailer
//        } catch (Exception $e) {
//            echo $e->getMessage(); exit;//Boring error messages from anything else!
//        }


        // captcha
//		$text = '杨志兵啊';
//		if (visitor::has_role('guest')) {
//			visitor::set_role_var('guest', 'captcha', $text);
//		} else {
//			visitor::set_role('guest', 0, 0, array('captcha' => $text));
//		}
//		$captcha = visitor::get_role_var('guest', 'captcha');
//		
//		dump($captcha);
//		
//		captcha::set_text($text);
//		captcha::set_noise_level(config::get('technique.captcha.noise_level', 1));
//		captcha::set_font_file(swap\share_dir . '/font/simfang.ttf');
//		captcha::set_size(150, 60);
//		visitor::no_cache();
//		visitor::set_role_var('guest', 'used', 0);
//		$used = visitor::get_role_var('guest', 'used');
//		dump($used);
//		self::send(captcha::generate('zh'), 'image/png');
//		$length = 4;
//		$cache = cache_pool::get_cache('captcha');
//		if ($cache->has('zh_cn_chars')) {
//			$chars = $cache->get('zh_cn_chars');
//		} else {
//			$chars = file_get_contents(swap\share_dir . '/captcha/' . 'zh_cn_chars.txt');
//			$cache->set('zh_cn_chars', $chars);
//		}
//		dump($chars);
//		$text = '';
//		while ($length-- > 0) {
//            $offset = hexdec(substr (random_sha1 (), 0, 4)) % iconv_strlen ($chars, 'UTF-8');
//            $text .= iconv_substr ($chars, $offset, 1, 'UTF-8');
//        }
//		dump($text);
//        return $text;

//        self::show_page();
    }

}
